﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cwtrock.dao.impl;
using cwtrock.model;
using cwtrock.syscommon;
using cwtrock.util;

namespace cwtrock.service.impl
{
    class AccountsServiceImpl
    {
        private SysDataDaoImpl sysDataDaoImpl = new SysDataDaoImpl();
        private AccountsDaoImpl accountsDaoImpl = new AccountsDaoImpl();


        // 获取会计科目类型的集合
        public List<SysData> getSysDataByType(string type) {

            return sysDataDaoImpl.getSysDataByType(type);
        }

        public int save(Accounts acct) {

            return accountsDaoImpl.save(acct);
          
        }

        public int update(Accounts acct)
        {

            return accountsDaoImpl.updateAccounts(acct);

        }

        public int del(string code)
        {
            return accountsDaoImpl.del(code);
        }

        public Accounts getAcctsByCode(string code)
        {
            return accountsDaoImpl.getAccountByCode(code);
        }

        // 按分类取得会计科目
        public List<Accounts> getAcctsByCatalog(string catalog)
        {
            return accountsDaoImpl.getAcctsByCatalog(catalog);
        }

        // 取该会计科目的子科目个数
        public int getChildCntByAcct(string acctcode)
        {
            return accountsDaoImpl.getChildCntByAcct(acctcode);
        }

        // 按科目代码取得科目名称
        public string getAcctnameByCode(string acctcode)
        {
            return accountsDaoImpl.getAcctnameByCode(acctcode);
        }

        // 按科目代码取得科目id
        public string getAcctidByCode(string acctcode)
        {
            return accountsDaoImpl.getAcctidByCode(acctcode);
        }

        // 获取全部的科目数据
        public List<Accounts> getAcctAll() {

            return accountsDaoImpl.getAcctAll();
        }

        // 取得科目的父级科目
        public Accounts getParentAcct(List<Accounts> acctls , Accounts acct) {

            if (acctls == null || acct == null)
                return null;
            foreach (Accounts pacct in acctls)
            {
                if (pacct.Acctcode.Equals(acct.Parentid))
                {
                    return pacct;
                }
            }
            return null;
        }

        // 取得科目名称
        public string getAcctname(List<Accounts> acctls, string acctcode)
        {

            if (acctcode == null || acctls == null)
                return null;

            string acctname = "";
            foreach (Accounts acct in acctls)
            {
                if (acctcode.Equals(acct.Acctcode))
                {

                    // 明细科目
                    if (SysConst.ACCT_ROOT.Equals(acct.Parentid))
                    {
                        acctname = acct.Acctname;

                    }
                    // 非明细科目 
                    else
                    {
                        Accounts pacct = acct;
                        while (!SysConst.ACCT_ROOT.Equals(pacct.Parentid))
                        {
                            if (SimpleTool.isEmpty(acctname))
                            {
                                acctname = pacct.Acctname;
                            }
                            else {
                                acctname = pacct.Acctname + SysConst.ACCT_NAME_SEPERATOR + acctname;
                            } 
                            pacct = getParentAcct(acctls, pacct);
                        }
                        acctname = pacct.Acctname + SysConst.ACCT_NAME_SEPERATOR + acctname;
                    }
                    break;
                }
            }
            return acctname;
        }
    }
}
